package com.xiyu.service.dal.job;

import com.xiyu.service.enums.mailbox.protocol.MailboxProtocolEnum;
import com.xiyu.service.framework.job.core.handler.JobHandler;
import com.xiyu.service.framework.postOffice.factory.PostOfficeFactory;
import com.xiyu.service.framework.postOffice.service.PostOfficeService;
import com.xiyu.service.model.mailbox.account.MailboxAccount;
import com.xiyu.service.repository.mailbox.account.MailboxAccountRepository;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;

import java.util.List;

@Component
@Slf4j
@DisallowConcurrentExecution
public class ReceiveMailContentJob implements JobHandler {

    @Resource
    MailboxAccountRepository mailboxAccountRepository;

    @Resource
    PostOfficeFactory postOfficeFactory;

    @Override
    public String execute(String param) throws Exception {
        List<MailboxAccount> accounts = mailboxAccountRepository.findByMailboxProtocolType(MailboxProtocolEnum.IMAP.getValue());
        log.info("开始收取邮件内容");
        for (MailboxAccount account : accounts){
            if(account.lastReceiveTime() == null)
                continue;
            PostOfficeService postOfficeService = postOfficeFactory.getInstance(account.id());
            postOfficeService.receiveAccountMailContentJob();
        }
        log.info("结束收取邮件内容");
        return "" ;
    }
}
